// pages/Refuelling/Refuelling.js
var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 500,
    buying:[],
    face_money:0,
    face_money1:300,
    recharge: [
      { money: '200' },
      { money: '300' },
      { money: '400' },
    ],
    buyindex:1
  },
  start_buycarorder(){
    app.navTo('../buycarorder/buycarorder')
  },
  start_rechargecar() {
    app.navTo('../rechargecar/rechargecar')
  },
  gm_buying(e){
    //console.log(e.currentTarget.dataset.buyid);
    var buyid = e.currentTarget.dataset.buyid;
    app.navTo('../bycard/bycard?buyid=' + buyid)
  },

  //选择充值
  start_chosere(e) {
    var index = e.currentTarget.dataset.index;
    var recharge = this.data.recharge;
    
    this.setData({
      buyindex: index,
      face_money1: recharge[index]['money']
    })
    // console.log(recharge[index]['money'])
  },
  inputPrice:function(e){
    var p = e.detail.value;
    this.setData({
      face_money:p
    })
  },
  formSubmit:function(e){ 
    //console.log(e.detail.value); 
    var buying_hao = e.detail.value.buying_hao;
    
    if (this.data.face_money != 0)
    {
      var face_money = this.data.face_money;
    } else{
      var face_money = this.data.face_money1;
    }
   
    var name = e.detail.value.name;
    var phone = e.detail.value.phone;

    if (buying_hao == ''){
      wx.showToast({
        title: '卡号不能为空',
        icon:'none',
        duration:2000
      })
      return false;
    }
    var that = this;
    if (name == '') {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (phone == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(true){
      util.commonRequest('pacevalue', 'post', { buying_hao: buying_hao, phone: phone, name: name, face_money: face_money }, function (res) {
        if (res.data.code == 200) {
          var id = res.data.data.id;
          var total = res.data.data.total;
          that.payMent(id, total);
        } else {
          wx.showToast({
            icon: 'loading',
            title: res.data.msg,
          })
        }
      });

    }

  },
  payMent: function (id, total) {
    var that = this;
    var param = {
      body: "加油卡充值",
      total: total,
      id: id,
      jykType: 2
    };
    util.commonRequest('XcuJyk', 'post', param, function (res) {
      if (res.data.code == 200) {
        wx.requestPayment({
          'timeStamp': '' + res.data.data.timeStamp + '',
          'nonceStr': '' + res.data.data.nonceStr + '',
          'package': '' + res.data.data.package + '',
          'signType': 'MD5',
          'paySign': '' + res.data.data.paySign + '',
          success: function (res) {
            console.log(res)
            wx.showToast({
              title: '支付成功',
              icon: 'loading'
            })
            // handler.intoPageHandler("../user-appointment-record/index");
          },
          fail: function (res) {
            wx.showToast({
              title: '支付失败',
              icon: 'loading'
            })
          }
        })
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var temp = this.data.tradeData;
    util.commonRequest('buyingcardlist', 'post', '', function (res) {
      if (res.data.code == 200) { 
        that.setData({
          buying: res.data.data
        }) 
        console.log(res.data.data)
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})